package com.itheima.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.Student;
import com.itheima.domain.User;
import com.itheima.service.Impl.UserServiceImpl;
import com.itheima.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @param
 * @return
 */
@WebServlet("/userServlet/*")
public class UserServlet extends BeanServlet {


    //创建业务层对象复用
    UserService service=new UserServiceImpl();
    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ObjectMapper om= new ObjectMapper();
       //调用业务层的查询所有方法
        List<Student> list=service.findAll();
        om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        //使用工具类还是插件什么的，先创建该对象，再调用方法将集合转为json类型的字符串写回前端
        String s = om.writeValueAsString(list);
        /*设置响应的字符集主要是确定json类型*/
        resp.setContentType("text/json;charset=UTF-8");
        //写回
        resp.getWriter().write(s);
    }
    protected void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ObjectMapper om= new ObjectMapper();
    //字符流读取前端发送的json格式的字符串
        BufferedReader reader = req.getReader();
        //将字符串转为json对象
        Student student = om.readValue(reader, Student.class);
        //调用业务层新增方法
        boolean flag = service.save(student);
        /*设置响应的字符集主要是确定json类型*/
        resp.setContentType("text/json;charset=UTF-8");
        //写回
        resp.getWriter().write(String.valueOf(flag));
    }
    protected void findByNum(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ObjectMapper om= new ObjectMapper();
    //获取页面的请求参数
        String number = req.getParameter("number");
        //调用业务层查询单个方法
        Student student = service.findByNum(number);
        String s = om.writeValueAsString(student);
        /*设置响应的字符集主要是确定json类型*/
        resp.setContentType("text/json;charset=UTF-8");
        //写回
        resp.getWriter().write(s);
    }
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ObjectMapper om= new ObjectMapper();
        //字符流读取前端发送的json格式的字符串
        BufferedReader reader = req.getReader();
        //将字符串转为json对象
        Student student = om.readValue(reader, Student.class);
        //调用业务层新增方法
        boolean flag = service.update(student);
        /*设置响应的字符集主要是确定json类型*/
        resp.setContentType("text/json;charset=UTF-8");
        //写回
        resp.getWriter().write(String.valueOf(flag));
    }
    protected void deleteByNum(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     //获取前端请求参数
        String number = req.getParameter("number");
        if(number!=null){
            //调用业务层批量删除方法
            boolean flag = service.deleteByNum(number);
            /*设置响应的字符集主要是确定json类型*/
            resp.setContentType("text/json;charset=UTF-8");
            //写回
            resp.getWriter().write(String.valueOf(flag));
        }

    }
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端请求参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        /*设置响应的字符集主要是确定json类型*/
        resp.setContentType("text/json;charset=UTF-8");
        //调用业务层方法
      User user=service.login(username,password);
        System.out.println("user = " + user);
      if(user!=null){
          //登录成功后将登录结果存进session中，以便过滤器判断
          HttpSession session = req.getSession();
          session.setAttribute("user",user);
          //调用工具类，将登录查询的结果转为json格式字符串写回前台
          String s = new ObjectMapper().writeValueAsString(true);
          resp.getWriter().write(s);
      }else {
          String s = new ObjectMapper().writeValueAsString(false);
          resp.getWriter().write(s);
      }

    }
    protected void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端请求参数
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");
        //调用业务层方法
       List<Student> list=service.selectByPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
       //把集合交给pageInfo处理
        PageInfo<Student> pageInfo = new PageInfo<Student>(list);
        //将分页对象交给json对象进行转换为字符串
        ObjectMapper om = new ObjectMapper();
        om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        String s = om.writeValueAsString(pageInfo);
        /*设置响应的字符集主要是确定json类型*/
        resp.setContentType("text/json;charset=UTF-8");
        resp.getWriter().write(s);
    }
}
